package com.fieldchain.ex.fa100.utils.net.action;

import com.fieldchain.ex.fa100.bean.event.BaseEvent;
import com.fieldchain.ex.fa100.utils.net.inter.BaseRequest;
import com.fieldchain.ex.fa100.utils.net.tools.MainHandler;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 文件描述：
 * 创 建 人：byl
 * 创建时间：2017/9/5 0005 18:13
 */
public class PostRequest {
    public static void postRequest(Map<String, String> params, String url, final BaseRequest re, OkHttpClient okHttpClient, final String tags) {

        //创建Form表单对象，可以add多个键值队
        FormBody.Builder builder = new FormBody.Builder();
        if (params != null) {
            Iterator<Map.Entry<String, String>> it = params.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> entry = it.next();
                builder.add(entry.getKey(), entry.getValue() + "");
            }
        }
        FormBody formBody = builder.build();
        //创建一个Request
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        //发送请求获取响应
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                MainHandler.getInstance().post(new Runnable() {
                    @Override
                    public void run() {
                        EventBus.getDefault().post(new BaseEvent(2,tags));
                        re.baseOnFaild(e,tags);
                    }
                });

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String str = response.body().string();
                MainHandler.getInstance().post(new Runnable() {
                    @Override
                    public void run() {
                        EventBus.getDefault().post(new BaseEvent(2,tags));
                        re.baseOnSucess(str,tags);
                    }
                });
            }
        });
    }


    public static void postRequest(String json, String url, final BaseRequest re, OkHttpClient okHttpClient, final String tags) {
        RequestBody jsonBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        //创建一个Request
        Request request = new Request.Builder()
                .url(url)
                .post(jsonBody)
                .build();
        //发送请求获取响应
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                MainHandler.getInstance().post(new Runnable() {
                    @Override
                    public void run() {
                        EventBus.getDefault().post(new BaseEvent(2,tags));
                        re.baseOnFaild(e,tags);
                    }
                });

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String str = response.body().string();
                MainHandler.getInstance().post(new Runnable() {
                    @Override
                    public void run() {
                        EventBus.getDefault().post(new BaseEvent(2,tags));
                        re.baseOnSucess(str,tags);
                    }
                });
            }
        });
    }
}
